package servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BasicServlet extends HttpServlet {

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        String methodAttribute = (String) request.getAttribute("method");
        Object result = null;
        try {
            Method method = this.getClass().getMethod(methodAttribute,HttpServletRequest.class,HttpServletResponse.class);
            result = method.invoke(this,request,response);

            if(result instanceof Object ){
                response.getWriter().print(result);
            }
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(result.equals("Success")) System.out.println(result.toString());
    }
}
